// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import config from "../config/config";
import MainGame from "../game/MainGame";
import func from "../config/func";
import AudioManage from "../manage/AudioManage";
import Guide from "../guide/Guide";
import AdManage from "../manage/AdManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameOver extends cc.Component {

    @property({ type: cc.Node, tooltip: "背景框" })
    m_bg: cc.Node = null;
    @property({ type: cc.Label, tooltip: "奖励数量文本" })
    m_reward: cc.Label = null;
    @property({ type: cc.Sprite, tooltip: "下一关按钮" })
    m_btn_next: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "重开按钮" })
    m_btn_again: cc.Sprite = null;
    @property({ type: cc.Sprite, tooltip: "" })
    m_title: cc.Sprite = null;
    @property({ type: cc.SpriteFrame, tooltip: "胜利" })
    m_spriteFrame_victory: cc.SpriteFrame = null;
    @property({ type: cc.SpriteFrame, tooltip: "失败" })
    m_spriteFrame_lose: cc.SpriteFrame = null;

    n_mainGame: MainGame = null;
    n_reward: number = 0;

    start() {
        this.n_mainGame = cc.find("Canvas/game").getComponent(MainGame);
    }

    init(complete: boolean, reward: number) {
        config.openPopup = false;
        if (config.guide) {
            Guide.instance.hideGuide(true);
        }
        if (config.gameType == "vivo") {
            func.creatrBanner();
            func.creatVivoNativeAd(false);
        } else if (config.gameType == "QQ" && config.sclY < 2) {
            AdManage.instance.initQQcreateBlockAd("landscape");
        }
        func.setNodeAni(this.m_bg);
        this.n_reward = reward;
        this.m_reward.string = this.n_reward.toString();
        config.money += this.n_reward / config.cutTime;
        if (complete) {
            AudioManage.audioManage.playEffectMusic(config.soundPath.victory);
            this.m_title.spriteFrame = this.m_spriteFrame_victory;
            config.checkPoint++;
            let date = config.dataJson.checkPoint[10000 + config.checkPoint];
            if (date) {
                this.m_btn_next.node.active = true;
                this.m_btn_again.node.active = false;
            } else {
                console.log("恭喜通关");
                config.checkPoint = 1;
                this.m_btn_next.node.active = false;
                this.m_btn_again.node.active = false;
            }
        } else {
            AudioManage.audioManage.playEffectMusic(config.soundPath.fail);
            this.m_title.spriteFrame = this.m_spriteFrame_lose;
            this.m_btn_next.node.active = false;
            this.m_btn_again.node.active = true;
        }
    }
    /**
     * 点击下一关
     */
    next() {
        if (config.gameType == "vivo") {
            func.destroyBanner();
        }else if (config.gameType == "QQ") {
            if (config.qq_loadCfg) {
                if (config.qq_loadCfg.mistouchOn == 0 || (config.qq_loadCfg.mistouchOn == 1 && config.qq_gameOver)) {
                    this.nextCheckPoint();
                } else {
                    config.qq_gameOver = true;
                    AdManage.instance.initRewardedVideoAd(config.qq_videoAd, (res) => {
                        if (res == 1 || res == 2) {
                            this.nextCheckPoint();
                        }
                    })
                }
            }else{
                this.nextCheckPoint();
            }
        } else {
            this.nextCheckPoint();
        }
    }
    nextCheckPoint(){
        if (config.gameType == "QQ" && config.sclY < 2) {
            AdManage.instance.n_blockAd.destroy();
        }
        func.goReduceOut(this.m_bg, () => {
            this.node.active = false;
            this.n_mainGame.reopen(true);
        })
    }
    /**
     * 再来一次 
     */
    again() {
        if (config.gameType == "QQ" && config.sclY < 2) {
            AdManage.instance.n_blockAd.destroy();
        }
        func.goReduceOut(this.m_bg, () => {
            this.node.active = false;
            this.n_mainGame.reopen(false);
        })
    }
    /**
     * 返回主界面
     */
    mainInterface() {
        if (config.gameType == "QQ" && config.sclY < 2) {
            AdManage.instance.n_blockAd.destroy();
        }
        func.goReduceOut(this.m_bg, () => {
            this.node.active = false;
            this.n_mainGame.backToStartScene();
        })
    }
    // close() {
    //     func.goReduceOut(this.m_bg, () => {
    //         this.node.active = false;
    //     })
    // }
}
